###################################################################################################################################################
### Setup 
setwd('~/Dropbox/Projects/Dissertation/Bill Text and Agenda Control/JOP Replication Files')
source('text_setup.R') 

# Load data
pvdfH <- readRDS('Data/predVotes_modData_House_rr.rds')
pvdfS <- readRDS('Data/predVotes_modData_Senate_rr.rds')
accH <- readRDS(file='Data/ModelAccuracyOverTime_House_rr.rds')
accS <- readRDS(file='Data/ModelAccuracyOverTime_Senate_rr.rds')
billAccH <- readRDS(file='Data/billAccuracy_House_rr.rds')
billAccS <- readRDS(file='Data/billAccuracy_Senate_rr.rds')

# Congress variables
pvdfH$cong <- str_split(pvdfH$id, '_') %>% lapply(., `[[`, 1) %>% num()
pvdfS$cong <- str_split(pvdfS$id, '_') %>% lapply(., `[[`, 1) %>% num()
congs <- unique(pvdfH$cong) %>% sort()



#####################################################################################################################################################
### Figure 1

# Baselines (votes)
baseline <- function(df){
  sapply(congs, function(cg){
    filter(df, cong == cg) %>%
      pull(dv) %>%
      table() %>%
      prop.table() %>%
      .[2] %>%
      return
  }) %>%
    mean()
}
baseH <- baseline(pvdfH)
baseS <- baseline(pvdfS) 

# Baselines (bills)
billBaseH <- 0.94
billBaseS <- 0.91

# Plot
setEPS()
postscript(file='Figures/fg1.eps', width=10, height=5)

par(mfrow = c(1,2), las=2)
plot(congs, accH[,'full'], xlab = 'Congress', ylab = 'Out-of-Sample Accuracy', ylim = c(0.6, 1), 
     main='Votes', type='b', pch=15, lty=1, xaxt='n')
axis(side=1, at=congs)
lines(congs, accS[,'full'], type='b', pch=17, lty=2)
abline(h=baseH, lty=1)
abline(h=baseS, lty=2)
legend(legend= c('House', 'Senate'),x='bottomleft', bty='n', pch=c(15, 17), lty=c(1, 2))

plot(congs, billAccH[[2]], xlab = 'Congress', ylab = 'Out-of-Sample Accuracy', ylim = c(0.6, 1), 
     type = 'b', pch = 15, lty = 1, main='Bill Outcomes', xaxt='n')
axis(side=1, at=congs)
lines(congs, billAccS[[2]], type = 'b', pch = 17, lty = 2)
abline(h=billBaseH, lty=1)
abline(h=billBaseS, lty=2)
legend(legend = c('House', 'Senate'), pch = c(15, 17), lty = c(1, 2), x='bottomleft', bty = 'n')

par(mfrow=c(1,1))

dev.off()



#####################################################################################################################################################
### Figure 2

# Plot function
plotAcc <- function(df){
  modTypes <- c('full', 'noDW', 'noPty', 'noWts')
  plot(congs, df[,'full'], type = 'n', xaxt='n', xlab = 'Congress', 
       ylab = 'Out-of-Sample Accuracy', ylim = c(0.7, 1))
  axis(side=1, at=num(congs), las=2)
  grid()
  ltys <- c(1, 2, 3, 6)
  cols <- brewer.pal(length(modTypes)+1, 'Greys')
  pchs <- c(15, 16, 17, 18)
  legend('bottomleft', legend = c('Full Model', 'No DW-NOM', 'No Party ID', 'No Doc2Vec'), 
         lty = ltys, pch = pchs, col = cols[2:length(cols)], bty = 'n')
  for(ii in 1:length(modTypes)){
    type <- modTypes[ii]
    lines(congs, df[,type], type = 'b', lty = ltys[ii], pch = pchs[ii], col = cols[ii+1])
  }
  if(identical(df, accH)){
    title(main = 'House')
    abline(h = baseH, lty = 2, lwd = 2)
  } else {
    title(main = 'Senate')
    abline(h = baseS, lty = 2, lwd = 2)
  }
  
}

# Plot
tiff(filename='Figures/fg2.tif', width=10, height=5, units='in', res=1200)
par(mfrow = c(1,2))
plotAcc(accH)
plotAcc(accS)
par(mfrow = c(1,1))
dev.off()